﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_ProcessDoingHistory")]
    public class P_ProcessDoingHistory : IEntity<P_ProcessDoingHistory>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public int Type { get; set; }
        public int ProjectID { get; set; }
        public int ProcessID { get; set; }
        public int ProcessStepID { get; set; }
        public int ChangeState { get; set; }
        public int DoDirection { get; set; }
        public DateTime DoingTime { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string DoingUser { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(1024)]
        public string DoingContent { get; set; }

        public override P_ProcessDoingHistory Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_ProcessDoingHistory Find(System.Linq.Expressions.Expression<Func<P_ProcessDoingHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.ProcessDoingHistory.FirstOrDefault(where);
                if (item != null)
                {
                    this.ChangeState = item.ChangeState;
                    this.DoDirection = item.DoDirection;
                    this.DoingContent = item.DoingContent;
                    this.DoingTime = item.DoingTime;
                    this.DoingUser = item.DoingUser;
                    this.ID = item.ID;
                    this.ProcessID = item.ProcessID;
                    this.ProcessStepID = item.ProcessStepID;
                    this.ProjectID = item.ProjectID;
                    this.Type = item.Type;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_ProcessDoingHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.ProcessDoingHistory.Any(where);
            }
        }
    }
}